<?php

namespace App\Transformers\Admin;

use League\Fractal\TransformerAbstract;

class CategoryTransformer extends TransformerAbstract
{
    public $availableIncludes = ['categoryGroup', 'user'];

    public function transform($category)
    {
        return [
            'id' => $category->id,
            'name' => $category->name,
            'flag' => $category->flag,
            'pid' => $category->pid,
            'status' => $category->status,
            'category_group_id' => $category->category_group_id,
        ];
    }

    public function includeCategoryGroup($category)
    {
        return $this->item($category->categoryGroup, new CategoryGroupTransformer());
    }

    public function includeUser($category)
    {
        return $this->item($category->user, new class extends TransformerAbstract {
            public function transform($user) {
                return [
                  'id' => $user->id,
                  'username' => $user->username
                ];
            }
        });
    }
}

